package com.frxuexi.lib_base.room

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext

class UseRepository(private val dataBase: AppDataBase) {
    companion object {
        @Volatile
        private var useRepository: UseRepository? = null

        fun getInstance(dataBase: AppDataBase) = useRepository ?: synchronized(this) {
            useRepository ?: UseRepository(dataBase = dataBase)
        }
    }

    val userDao = dataBase.userDao()

    fun registerUser(user: User): Flow<Long> {
        return flow { emit(userDao.registerUser(user = user)) }
    }

    fun login(name:String,pass:String): Flow<User?> {
        return flow { emit(userDao.login(name, pass)) }
    }
}